package com.cloud.customtoastdemo

import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.cloud.customtoastdemo.contants.Constants
import com.cloud.customtoastdemo.toast.EasyToast
import com.cloud.customtoastdemo.utils.CountDownUtils.countDownCoroutines
import kotlinx.coroutines.Job

class MainActivity : AppCompatActivity() {

    private var mCountdownJob: Job? = null
    private val TAG = "MainActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        initViews()
    }

    private fun initViews() {
        val textView = findViewById<TextView>(R.id.tv_test)
        textView.setOnClickListener {
            mCountdownJob = countDownCoroutines(10, lifecycleScope,
                onTick = { second ->
                    Log.d(TAG, "toast显示时间$second")
                    textView.text = buildString {
                        append(second)
                        append("s后重发")
                    }
                }, onStart = {
                    // 倒计时开始
                }, onFinish = {
                    // 倒计时结束，重置状态
                    textView.text = buildString {
                        append("发送验证码")
                    }
                })

            EasyToast.showCustomToast(
                this@MainActivity,
                message = buildString {
                    append("祝大家国庆节快乐，万事如意")
                },
                duration = Constants.TOAST_SHOW_TIME,
                gravity = Gravity.TOP
            )
        }
    }

}